package com.eatsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eatsystem.common.BaseContext;
import com.eatsystem.common.Result;
import com.eatsystem.entity.ShoppingCar;
import com.eatsystem.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCar> add(@RequestBody ShoppingCar shoppingCart) {
        log.info("购物车数据：{}", shoppingCart);

        //设置用户id，指定当前用户是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCar::getUserId, currentId);

        if (dishId != null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCar::getDishId, dishId);
        } else {
            //添加到购物车是套餐
            queryWrapper.eq(ShoppingCar::getSetmealId, shoppingCart.getSetmealId());
        }

        //Sql:select * from shopping_cart where id = ? and dish_id/setmeal_id = ?
        ShoppingCar cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (cartServiceOne != null) {
            //如果已经存在，在原来的数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加购物车，数量默认1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return Result.success(cartServiceOne);
    }

    /**
     * 修改购物车商品
     *
     * @return
     */
    @PostMapping("/sub")
    public Result<String> sub(@RequestBody ShoppingCar shoppingCart) {
        log.info("id：{}", shoppingCart);

        //设置用户id，指定当前用户是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //构造查询条件
        LambdaQueryWrapper<ShoppingCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCar::getUserId, BaseContext.getCurrentId());

        //查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        //查询当前是菜品还是套餐
        if (dishId != null) {
            queryWrapper.eq(ShoppingCar::getDishId, dishId);
        } else {
            queryWrapper.eq(ShoppingCar::getSetmealId, shoppingCart.getSetmealId());
        }
        //SQL:select * from shopping_cart where id = ? and dish_id/setmeal_id = ?
        ShoppingCar cartServiceOne = shoppingCartService.getOne(queryWrapper);

        //如果在购物车中已经存在该商品或他敖仓，其数量-1
        if (shoppingCartService != null) {
            //该商品存在
            //SQL:select * from shopping_cart where id = ? and dish_id/setmeal_id = ? and number > 1
            queryWrapper.gt(ShoppingCar::getNumber, 1);
            ShoppingCar gtNumber = shoppingCartService.getOne(queryWrapper);
            if (gtNumber != null) {
                Integer number = cartServiceOne.getNumber();
                cartServiceOne.setNumber(number - 1);
                shoppingCartService.updateById(cartServiceOne);
            } else {
                shoppingCartService.removeById(cartServiceOne);
            }
        } else {
            return Result.error("未知错误");
        }
        return Result.success("删减订单成功");
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCar>> list() {
        log.info("查看购物车");

        //条件查询
        LambdaQueryWrapper<ShoppingCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCar::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCar::getCreateTime);
        List<ShoppingCar> list = shoppingCartService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCar::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);

        return Result.success("清空成功");
    }
}
